﻿@page "/pages/contacts/view/{id:int}"
@using CleanArchitecture.Blazor.Application.Features.Contacts.Commands.Delete
@using CleanArchitecture.Blazor.Application.Features.Contacts.DTOs
@using CleanArchitecture.Blazor.Application.Features.Contacts.Queries.GetById
@inherits MudComponentBase
@inject IStringLocalizer<Contacts> L
@attribute [Authorize(Policy = Permissions.Contacts.View)]
<PageTitle>@Title</PageTitle>
<Breadcrumbs  OnGoEditClick="GoEdit" OnDeleteClick="Delete" BreadcrumbItems="_breadcrumbItems"></Breadcrumbs>
<MudContainer Class="mt-3" MaxWidth="MaxWidth.Large">
@if (_model != null)
{
    <MudCard Class="pa-2">
        <MudCardHeader>
            <CardHeaderContent>
                 <MudText Typo="Typo.h6">@Title</MudText>
            </CardHeaderContent>
        </MudCardHeader>
        <MudCardContent>
            <MudGrid Spacing="3" Class="readonly-grid">
                <MudItem xs="12" md="6"> 
                        <ReadOnlyFieldX6 Label="@L[_model.GetMemberDescription(x=>x.Name)]" Value="_model.Name"></ReadOnlyFieldX6>
                </MudItem> 
<MudItem xs="12" md="6"> 
                        <ReadOnlyFieldX6 Label="@L[_model.GetMemberDescription(x=>x.Description)]" Value="_model.Description"></ReadOnlyFieldX6>
                </MudItem> 
<MudItem xs="12" md="6"> 
                        <ReadOnlyFieldX6 Label="@L[_model.GetMemberDescription(x=>x.Email)]" Value="_model.Email"></ReadOnlyFieldX6>
                </MudItem> 
<MudItem xs="12" md="6"> 
                        <ReadOnlyFieldX6 Label="@L[_model.GetMemberDescription(x=>x.PhoneNumber)]" Value="_model.PhoneNumber"></ReadOnlyFieldX6>
                </MudItem> 
<MudItem xs="12" md="6"> 
                        <ReadOnlyFieldX6 Label="@L[_model.GetMemberDescription(x=>x.Country)]" Value="_model.Country"></ReadOnlyFieldX6>
                </MudItem> 

            </MudGrid>
        </MudCardContent>
        <MudCardActions></MudCardActions>
    </MudCard>
 }
</MudContainer>
 

@code {
    public string? Title { get; private set; }
    [Parameter]
    public int Id { get; set; }
    private List<BreadcrumbItem>? _breadcrumbItems;
    private ContactDto? _model;
    protected override async Task OnInitializedAsync()
    {
        Title = L["Contact"];
        _breadcrumbItems = new List<BreadcrumbItem>
        {
            new BreadcrumbItem(L["Home"], href: "/"),
            new BreadcrumbItem(L["Contacts"], href: "/pages/contacts")
        };
        var result = await Mediator.Send(new GetContactByIdQuery() { Id = Id });
        result.Map(data =>
        {
            _model = data;
            return data;
        }).Match(data =>
        {
            _breadcrumbItems.Add(new BreadcrumbItem(data.Name, null, disabled: true));
        }, errors =>
        {
            Snackbar.Add(errors, MudBlazor.Severity.Error);
        });

    }
    void GoEdit()
    {
        Navigation.NavigateTo($"/pages/Contacts/edit/{Id}");
    }
    async Task Delete()
    {
        var contentText = string.Format(ConstantString.DeleteConfirmation, _model!.Name);
        var command = new DeleteContactCommand(new int[] { _model.Id });
        await DialogServiceHelper.ShowDeleteConfirmationDialogAsync(command, ConstantString.DeleteConfirmationTitle, contentText, async () =>
        {
            await InvokeAsync(() =>
            {
                Navigation.NavigateTo($"/pages/Contacts");
            });
        });
    }
}
